package com.baozhun.mall.common.widget.dialog

import android.content.Context
import android.view.View
import com.baozhun.mall.common.R
import com.baozhun.mall.common.adapter.CommonChooseAdapter
import com.baozhun.mall.common.databinding.DialogCommonChooseBinding
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener

/**
 * @Title：通用的滚轮选择器
 * @Description：
 * @Author：xuefeng.zhu
 * @Since：2022/12/2
 * @Version：1.0.0
 */
class CommonChooseDialog(
    context: Context,
    var titleResId: Int,
    var selectItem: String? = "",
    private var dataList: MutableList<String>,
    private var selectCallBack: (selectItem: String, selectPosition: Int) -> Unit
) : BaseBottomPopupView<DialogCommonChooseBinding>(context), View.OnClickListener, OnItemClickListener {

    private var mSelectPosition: Int = -1
    private var mSelectItem: String = ""
    private val mCommonChooseAdapter by lazy { CommonChooseAdapter(dataList) }

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_common_choose
    }

    override fun onCreate() {
        super.onCreate()
        mBinding.title = context.getString(titleResId)
        mBinding.listener = this
        mBinding.rvChoose.adapter = mCommonChooseAdapter
        mCommonChooseAdapter.setOnItemClickListener(this)
        //赋默认值
        mSelectPosition = dataList.indexOf(selectItem)
        if (mSelectPosition != -1) {
            mSelectItem = dataList[mSelectPosition]
            mCommonChooseAdapter.updateChooseStatus(mSelectPosition)
        }
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        when (v?.id) {
            R.id.iv_close -> dismiss()
        }
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        mSelectPosition = position
        mSelectItem = dataList[position]
        mCommonChooseAdapter.updateChooseStatus(position)
        selectCallBack.invoke(mSelectItem, mSelectPosition)
        dismiss()
    }
}